package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;

import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

/**
 * @author 贾育权~
 * @version 1.0
 * 添加购物车的实现的方法
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    //@Autowired
    //BeanUtils beanUtils;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealDishMapper setMealService;
    @Autowired
    private SetmealMapper setmealMapper;


    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {//添加购物车的方法
        //购物车的字段表
        //购物车表的字段 // id name image  user_id dish_id setmeal_id dish_flavor
        //number amount create_time //就是这样的大概的意思 //我们对应到这个类中
        //享购物车里添加数据
        //1.就是购物车表中已经存在{套餐还是菜品}
        //shoppingCartMapper.add(shoppingCartDTO);
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);//四个属性对应赋值
        //获取当前用户的id
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);//对应的就是哪一个用户的购物车


        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);//

        if (list != null && list.size() > 0) {//如果数据库里面有的话
            //执行我们的更新操作，说白了就是数据库里有数据，我们就是更新数量，否则就是插入数据
            //  BaseContext.setCurrentId(shoppingCartDTO.getUserId());
            ShoppingCart cartServiceOne = list.get(0);//对应的就是一条对应的购物车数据
            Integer number = cartServiceOne.getNumber();
            cartServiceOne.setNumber(number + 1);//我们就是把这个对象的
            shoppingCartMapper.updateNumberById(cartServiceOne);

        } else {//如果就是不存在就是，我们就是把整条数据//添加的是菜品 添加的是套餐
            //我们需要就是把对应的实体的对象补齐之后加入的我们的数据库
            Long dishId = shoppingCartDTO.getDishId();
            Long setmealId = shoppingCartDTO.getSetmealId();
            if (dishId != null) {//菜品的处理逻辑//菜品的id
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());
                //  ShoppingCart//就是补充
                shoppingCart.setDishId(dish.getId());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());

                //
               /* shoppingCart.setNumber(1);//就是在1//购物车的一条数据对象进行操作

                shoppingCart.setCreateTime(LocalDateTime.now())*/
                ;


            } else {//目前我们对应的就两个 菜品 和套餐
                Setmeal setmeal = setmealMapper.getById(setmealId);///就是我们的套餐的对象
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());

                //面对所有的情框我们的


            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            //shoppingCartMapper.///
            shoppingCartMapper.insert(shoppingCart);

        }


        //不存在就插入一条购物车数据


    }

    @Override
    public List<ShoppingCart> showShoppingCart() {//
        // BaseContext.setCurrentId(BaseContext.getCurrentId());
        Long currentId = BaseContext.getCurrentId();//获取id
        //微信用户的id
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(currentId)
                .build();//通过构造器的方法创建一个对象//构建一个对象//购物车的数据全部都查出来

        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        if (CollectionUtils.isEmpty(list)) {
            return Collections.EMPTY_LIST;
        }
        return list;
    }
        /*
        * 清空购物车
        *
        * */
    @Override
    public void clean() {
        //获取当前用户的id，关于这个购物车表的id
        Long currentId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteByUserId(currentId);

    }

    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {//根据id删除
        //在数据库存在但是数量是1 的时候，就删除，就把这条数据


       //在数据库里面存在是数量不是1就是执行修改操作
        ShoppingCart shoppingCart = new ShoppingCart();//
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        Long currentId = BaseContext.getCurrentId();//独立的线程空间啥的
        shoppingCart.setUserId(currentId);//这是后的购物车对应的数据对象有两个筛选条件，一个是user_id，一个是dish_id/套餐的id
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);//
        if (list != null && list.size() > 0) {
            ShoppingCart shoppingCart1 = list.get(0);//我们就是从数据库查上来的购物车数据
            Integer number = shoppingCart1.getNumber();
            if(number==1){//
                shoppingCartMapper.deleteById(shoppingCart1.getId());
            }else{
                shoppingCart1.setNumber(number-1);
                shoppingCartMapper.updateNumberById(shoppingCart1);
            }

            }


        }




    }


